﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Diagnostics;

namespace MyEngineExercise.我的扩展
{
    public static class CursorStyle
    {
        private static String[] _names = null;//所有资源的名称
        private static Dictionary<String, Cursor> _cursorDict = new Dictionary<string, Cursor>();

        public static Cursor GetCursor(String name)
        {
            return _cursorDict.ContainsKey(name) ? _cursorDict[name] : null;
        }


        /// <summary>
        /// 静态构造函数
        /// </summary>
        static CursorStyle()
        {
            //注意这里要 点选要用的资源文件-》属性-》生成操作-》嵌入资源
            _names = Assembly.GetExecutingAssembly().GetManifestResourceNames();
            //foreach (var name in _names)
             //   Debug.Print(name.ToString());//MyEngineExercise.我的扩展.窗体.FeatureLayerInfoForm.resources

            Regex regex = new Regex(Constants.CURSOR_REGEX);//用正则表达式过滤文件名

            foreach (String name in _names.Where(v => regex.IsMatch(v)))
            {
                //Debug.Print("1 cursor");

                // 光标文件可能不可用，因此会产生导常
                try
                {
                    Cursor cursor = new Cursor(Assembly.GetExecutingAssembly().GetManifestResourceStream(name));
                    Match match = regex.Match(name);

                    _cursorDict.Add(match.Groups["curName"].Value, cursor);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("resources文件夹，cur文件异常。\n" + ex.Message);
                }
            }
        }
    }
}
